package com.rimi.interceptor;

import com.rimi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @program: Day09SpringSSM
 * @Date: 2018/8/29 10:42
 * @Author: Mr.GuoQiang
 * @Description:
 */

/*创建了一个拦截器 类  实现HandlerInterceptor*/
/*也可以实现implements WebRequestInterceptor 不常用*/
public class TestInteracptor implements HandlerInterceptor {

    @Autowired
    private UserService userService;
    /*
     * 过滤器和拦截器的区别
     *   1、创建类的方法
     *   Filter    HandlerInterceptor
     *   2、依赖不一样
     *     Filter 依赖于 servlet容器 tomcat
     *     Interceptor 依赖Spring ioc容器
     *   3、配置位置
     *       web.xml
     *       applicationContext.xml spring配置文件
     * */

    //在被拦截的controller之前调用
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("1111111111111111111111"+handler);
        return true;  //放行
    }

    //该方法是在页面响应之前被调用的
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//        modelAndView.setViewName("/catalog");
        System.out.println("22222222222222");
    }

    //是在页面响应以后被触发、调用的
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("33333333333333"+ex);
    }




}


